/**
 * 
 */
package org.framework2.test.webserver;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

/**
 * <p>
 * 测试桩HTTP服务器类
 * </p>
 * @see 
 * @author ahli
 * @version WebServerTestMockV1, 2010-8-19
 * @since WebServerTestMockV1
 */
public class WSTMServer {

	/**
	 * 
	 */
	private static final int LOGS = 10;

	/**
	 * web应用服务上下文
	 */
	private static final String WEB_CONTEXT = "/testCase";
	
	/**
	 * 日志对象
	 */
	private static final Log log = LogFactory.getLog(WSTMServer.class);
	
	/**
	 * http处理器
	 */
	private HttpHandler httpHandler;
	
	/**
	 * 端口，默认8888
	 */
	private int port = 8888;
	
	/**
	 * 上下文
	 */
	private String context = WEB_CONTEXT;
	
	/**
	 * 线程，默认10
	 */
	private int thread = 10;
	
	/**
	 * 启动应用服务器
	 * @throws IOException
	 */
	public void start() throws IOException {
		long t1 = System.currentTimeMillis();
		log.info("构建测试桩服务");
		log.info("设置测试桩服务作用的端口：" + port);
		HttpServer hs = HttpServer.create(new InetSocketAddress(port), LOGS);
		log.info("设置测试桩服务上下文：" + context);
		hs.createContext(context, httpHandler);
		hs.setExecutor(Executors.newFixedThreadPool(thread));
		log.info("启动测试桩服务");
		hs.start();
		log.info("测试桩服务启动成功。--耗时:" + (System.currentTimeMillis() - t1) + "毫秒。");
	}

	/**
	 * 设置HTTP处理器
	 * @param httpHandler
	 */
	public void setHttpHandler(HttpHandler httpHandler) {
		this.httpHandler = httpHandler;
	}

	/**
	 * 设置端口
	 * @param port
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * 设置web上下文
	 * @param context
	 */
	public void setContext(String context) {
		this.context = context;
	}

	/**
	 * 设置线程池大小
	 * @param thread
	 */
	public void setThread(int thread) {
		this.thread = thread;
	}
}
